#!/bin/sh

# 16.09.2007 Volker Quetschke
# Check that inferred makefiles for .INCLUDE do not disturb the dependency
# checking afterwards.
# (issue 81296)

: ${DMAKEPROG:=dmake}
file1="mfile1.mk"
file2="my.c"
file3="my.obj"
file4="my.foo"
tmpfiles="$file1 $file2 $file3 $file4"

trap '{ echo "trapped signal - removing temporary files" ; rm -rf $tmpfiles ; }' 1 2 3 15

# Remove files from prior failed run
rm -rf $tmpfiles

# Remember to quote variables in generated makefiles( $ -> \$ ).
# Test 1 - from testcase t_81296_5.mk
cat > $file1 <<EOT
SHELL*:=/bin/sh
SHELLFLAGS*:=-ce

# Test that my.c is build when my.foo is newer.

# rules.mk ---
%.obj : %.c ;  @+printf "\$@:"
#	@sleep 1
	@touch \$@

%.dpo : %.c ; @+printf "incbuild:\$@:"
#	@sleep 1

# target.mk ---
all : my.obj ; @echo all

# If the file exist no inference is done for it.
.INCLUDE .IGNORE : my.dpo

# When inference was run on the %.dpo with an existing
# %.c these rule gets ignored.
# local makefile.mk ---
%.c : %.foo ; @+printf "\$@:"
#	@sleep 1
	@touch \$@
EOT

# Create test environment
touch my.c
sleep 1
touch my.foo

output1=`eval ${DMAKEPROG} -rf $file1 2>&1 `
result1=$?

if test $result1 = 0 -a "$output1" = "incbuild:my.dpo:my.c:my.obj:all"; then
  echo "Subtest 1: OK"
else
  echo "Subtest 1: Wrong result: $output1"
  echo
  result1=1
fi 

# Remove files from prior run
rm -rf $tmpfiles
# Remember to quote variables in generated makefiles( $ -> \$ ).
# Test 2 - from testcase t_81296_6.mk
cat > $file1 <<EOT
SHELL*:=/bin/sh
SHELLFLAGS*:=-ce

# Test that no circular dependency error is issued:
# $ rm -f my.* ; touch my.c ; ./dmake/dmake.exe -rf t_81296_6.mk

# rules.mk ---
%.obj : %.c ;  @+printf "\$@:"
	@touch \$@

%.dpo : %.c ; @+printf "incbuild:\$@:"

# target.mk ---
all : my.obj ; @echo all

# If the file exist no inference is done for it.
.INCLUDE .IGNORE : my.dpo

%.c : %.foo ; @+printf "\$@:"
	@touch \$@

# This leads to a (wrong) circular dependency error
my.obj : my.c

EOT

# Create test environment
touch my.c
sleep 1
touch my.foo

output2=`eval ${DMAKEPROG} -rf $file1 2>&1 `
result2=$?

if test $result2 = 0 -a "$output2" = "incbuild:my.dpo:my.c:my.obj:all"; then
  echo "Subtest 2: OK"
else
  echo "Subtest 2: Wrong result: $output2"
  echo
  result2=1
fi 


if test $result1 -eq 0 -a $result2 -eq 0 ; then
  echo "Success - Cleaning up"
  rm -rf $tmpfiles
  exit
else
  echo "Failure!"
  exit 1
fi
